package com.cdbkj.cloud.pos.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.androidyang.library.common.adapter.CommonAdapter;
import com.androidyang.library.common.adapter.Wrapper;
import com.androidyang.library.common.utils.ToastUtils;
import com.androidyang.library.http.okhttp.bean.HttpResult;
import com.androidyang.library.http.okhttp.listener.HttpClientHandler;
import com.cdbkj.cloud.pos.R;
import com.cdbkj.cloud.pos.http.HttpRequest;
import com.cdbkj.cloud.pos.utils.Tools;
import com.cdbkj.cloud.pos.bean.JsonResult;

import java.util.List;
import java.util.Map;

public class RegDateFragment extends Fragment implements AdapterView.OnItemClickListener, HttpClientHandler {
    private static final String ARG_PARAM1 = "userId";
    private static final String ARG_PARAM2 = "regTypeId";
    private String userId;
    private String regTypeId="";
    protected View rootView;
    protected GridView mGridView;
    private GridAdapter adapter;

    List<Map> list;

    private OnRegDateFragmentInteractionListener mListener;

    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Tools.dismissDialog();
            Tools.backgroundAlpha(getActivity(), 1);
            mListener.onRegReturnRegDate();
        }
    };


    public RegDateFragment() {

    }

    public static RegDateFragment newInstance(String userId,String regTypeId) {

        RegDateFragment fragment = new RegDateFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, userId);
        args.putString(ARG_PARAM2, regTypeId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userId = getArguments().getString(ARG_PARAM1);
            regTypeId= getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_reg_date, container, false);
        initView(rootView);
        return rootView;
    }

    private void initView(View rootView) {
        mGridView = (GridView) rootView.findViewById(R.id.regDate_gridView);
        //mGridView.setAdapter(new ListAdapter(getActivity(), Arrays.asList("1","1","1","1","1","1","1")));
        mGridView.setOnItemClickListener(this);
        HttpRequest.regDate("regDate",this,userId,regTypeId);
        adapter = new GridAdapter(getActivity(), null);
        mGridView.setAdapter(adapter);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnRegDateFragmentInteractionListener) {
            mListener = (OnRegDateFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Map item = (Map) adapterView.getAdapter().getItem(i);
        if(item.get("regDate")==null){
            //ToastUtils.showShort(getActivity(),"无挂号日期");
            Tools.showDialogWithReturnMainActivity("无挂号日期，请联系管理员！",getActivity());
            return;
        }
        mListener.onRegDateFragmentInteraction(userId,item.get("regDate").toString(),regTypeId);
    }

    @Override
    public void httpClientSuccess(Object o, HttpResult httpResult) {
        JsonResult json = (JsonResult) httpResult.getResult();
        if (json == null) {
            //ToastUtils.showShort(getActivity(),"服务器处理异常");
            Tools.showDialogWithReturnMainActivity("服务器处理异常，请联系管理员！",getActivity());
            return;
        }
        if (!json.isSuccess()) {
            Tools.showDialog(json.getMsg(), getActivity(), clickListener, clickListener);

            return;
        }
        list = (List) json.getData();
        adapter.setList(list,true);
    }


    @Override
    public void httpClientError(Object o, HttpResult httpResult) {
        //ToastUtils.showShort(getActivity(),"网络异常");
        Tools.showDialogWithReturnMainActivity("网络连接异常，请联系管理员！",getActivity());
    }

    public interface OnRegDateFragmentInteractionListener {
        void onRegReturnRegDate();
        void onRegDateFragmentInteraction(String userId,String regDate,String regTypeId);
    }

    class GridAdapter extends CommonAdapter<Map> {

        public GridAdapter(Context context, List<Map> list) {
            super(context, R.layout.item_reg_date, list);
        }


        @Override
        public void convert(Wrapper<Map> wrapper, Map item) {
            String weekDay = (String) item.get("weekDay");
            wrapper.setText(R.id.dateTitle_tv, (String) item.get("dateTitle"));
            wrapper.setText(R.id.day_tv, (String) item.get("day"));
            wrapper.setText(R.id.weekDay_tv, weekDay);
        }
    }
}
